package com.yimaxiaoerlang.module_signalling.utils;

import com.yimaxiaoerlang.module_signalling.core.YMMessageType;
import com.yimaxiaoerlang.module_signalling.model.YMRTMMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;

public class MessageUtils {
    /**
     * 创建点对点消息
     */
    public static String createPeerMessage(String uid, String tid, String message) {

        YMRTMMessage socketMessage = new YMRTMMessage();
        socketMessage.setUid(uid);
        socketMessage.setTid(tid);
        socketMessage.setMsgType(YMMessageType.RTMMessageTypePeerToPeer);
        socketMessage.setData(message);
        return tojson(socketMessage);
    }

    /**
     * 创建加入房间消息
     */
    public static String createJoinMessage(String uid, String channelId, String message) {
        YMRTMMessage socketMessage = new YMRTMMessage();
        socketMessage.setUid(uid);
        socketMessage.setChannelId(channelId);
        socketMessage.setMsgType(YMMessageType.RTMMessageTypeJoinChannel);
        socketMessage.setData(message);
        return tojson(socketMessage);
    }

    /**
     * 创建离开房间消息
     */
    public static String createLeaveMessage(String uid, String channelId, String message) {
        YMRTMMessage socketMessage = new YMRTMMessage();
        socketMessage.setUid(uid);
        socketMessage.setChannelId(channelId);
        socketMessage.setMsgType(YMMessageType.RTMMessageTypeLeaveChannel);
        socketMessage.setData(message);
        return tojson(socketMessage);
    }

    /**
     * +
     * 创建房间内消息
     */
    public static String createChannelMessage(String uid, String channelId, String message) {
        YMRTMMessage socketMessage = new YMRTMMessage();
        socketMessage.setUid(uid);
        socketMessage.setChannelId(channelId);
        socketMessage.setMsgType(YMMessageType.RTMMessageTypeChannel);
        socketMessage.setData(message);
        return tojson(socketMessage);
    }

    /**
     * 创建房间内sdp消息
     *
     * @param uid
     * @param channelId
     * @param message
     * @return
     */
    public static String createChannelVoIPMessage(String uid, String channelId, String message) {
        YMRTMMessage socketMessage = new YMRTMMessage();
        socketMessage.setUid(uid);
        socketMessage.setChannelId(channelId);
        socketMessage.setMsgType(YMMessageType.RTMMessageTypeVoIP);
        socketMessage.setData(message);
        return tojson(socketMessage);
    }

    private static String tojson(YMRTMMessage message) {
        JSONObject obj = new JSONObject();
//        Class clazz = message.getClass();
//        while (clazz != Object.class) {
//            //向上循环 遍历父类
//            Field[] field = clazz.getDeclaredFields();
//            for (Field f : field) {
//                f.setAccessible(true);
//                try {
//                    System.out.println("属性：" + f.getName() + " 值：" + f.get(message).toString());
//                    obj.put(f.getName(), f.get(message));
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//            clazz = clazz.getSuperclass();
//        }
        Class<?> clazz = message.getClass();
        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {//向上循环 遍历父类
            Field[] field = clazz.getDeclaredFields();
            for (Field f : field) {
                f.setAccessible(true);
                try {
                    Object ob=f.get(message);
                    if (ob!=null){
                        System.out.println("属性："+f.getName()+" 值："+ob==null?"null":ob.toString());
                        obj.put(f.getName(), ob.toString());
                    }else {

                        obj.put(f.getName(),"");
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
        return obj.toString();
    }


}
